package com.mallcloud.mall.app.controller;

import com.mallcloud.mall.app.api.entity.dto.WxPay2UnifiedOrderInfoDTO;
import com.mallcloud.mall.app.api.entity.vo.WxPay2UnifiedOrderResponseVO;
import com.mallcloud.mall.app.service.AppOrderCouponService;
import com.mallcloud.mall.app.service.AppWxPay2Service;
import com.mallcloud.mall.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_1
 */
@Api(tags = "app-微信v2支付")
@RestController
@AllArgsConstructor
@RequestMapping("/app/wxPay2")
@ConditionalOnProperty("const.wxpay.v2.appId")
public class AppWxPay2Controller {

	private final AppWxPay2Service appWxPayService;
	private final AppOrderCouponService appOrderCouponService;

	@ApiOperation("微信统一下单")
	@PostMapping("/unifiedOrder")
	public R<WxPay2UnifiedOrderResponseVO> unifiedOrder(@RequestBody WxPay2UnifiedOrderInfoDTO unifiedOrderInfo) {
		return R.ok(appWxPayService.unifiedOrder(unifiedOrderInfo));
	}

	//https://www.cnblogs.com/codertl/p/16151243.html
	//https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7&index=8
	@ApiOperation("异步通知接收")
	@PostMapping("/payNotify")
	public String payNotify(@RequestBody String xmlParams) {
		return appWxPayService.payNotify(xmlParams);
	}

	@ApiOperation("回调失败补偿")
	@PostMapping("/compensate")
	public void compensate(@RequestBody List<Long> ids) {
		appOrderCouponService.compensate(ids);
	}
}
